<template>
    <section>

    <!--查询UI-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="filters">
        <el-form-item>
            <el-input v-model="filters.name" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" v-on:click="getUsers">查询</el-button>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        </el-form>
    </el-col>

    <!--列表UI-->
    <el-table :data="list" ref="listTable"
    :cell-style="{padding:'2px'}"
     highlight-current-row  @selection-change="selsChange" style="width: 100%;">
        <el-table-column  type="selection"  width="55">
        </el-table-column>
        <el-table-column type="index" :index="getIndex" width="60px">
        </el-table-column >

                 <el-table-column prop="name" label="姓名" sortable>
                </el-table-column>
                 <el-table-column prop="addr" label="地址" width="300px" sortable>
                </el-table-column>
                 <el-table-column prop="age" label="年龄" sortable>
                </el-table-column>
                 <el-table-column prop="birth" label="出生日期" sortable>
                </el-table-column>
                <el-table-column prop="sex" label="性别" :formatter="formatSex" sortable>
                </el-table-column>

        <el-table-column label="操作" width="200">
        <template slot-scope="scope">
            <el-button-group>
            <el-button type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger"  @click="handleDel(scope.$index, scope.row)">删除</el-button>
          </el-button-group>
        </template>
        </el-table-column>
    </el-table>

    <div style="padding:10px">
    <!--批量删除UI-->
    <el-col :span="24" >
        <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
        <el-pagination layout="total,prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
        </el-pagination>
    </el-col>
    </div>

    <!--编辑UI-->
    <el-dialog title="编辑" :visible.sync="editFormVisible" width="440px" :close-on-click-modal="false">
        <el-form :model="editForm" label-width="80px" :rules="formRules" ref="editForm">

                    <el-form-item label="姓名" prop="name">
                        <el-input v-model="editForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="地址" prop="addr">
                        <el-input v-model="editForm.addr" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="年龄" prop="age">
                        <el-input v-model="editForm.age" auto-complete="off"></el-input>
                    </el-form-item>

             <el-form-item label="出生日期" prop="birth">
            <el-date-picker type="date" placeholder="出生日期" v-model="editForm.birth"></el-date-picker>
                    </el-form-item>

                    <el-form-item label="性别" prop="sex">

            <el-select v-model="editForm.sex" placeholder="性别">
            <el-option label="男"  :value="0"></el-option>
            <el-option label="女"  :value="1"></el-option>
            </el-select>
                    </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" >提交</el-button>
        </div>
    </el-dialog>

    <!--新增UI-->
    <el-dialog title="新增" :visible.sync="addFormVisible" width="440px" :close-on-click-modal="false">
        <el-form :model="addForm" label-width="80px" :rules="formRules" ref="addForm">

                 <el-form-item label="姓名" prop="name">
                        <el-input v-model="addForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="地址" prop="addr">
                        <el-input v-model="addForm.addr" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="年龄" prop="age">
                        <el-input v-model="addForm.age" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="出生日期" prop="birth">
            <el-date-picker type="date" placeholder="出生日期" v-model="addForm.birth"></el-date-picker>
                    </el-form-item>

             <el-form-item label="性别">

            <el-select v-model="addForm.sex" placeholder="性别">
                <el-option label="男"  :value="0"></el-option>
                <el-option label="女"  :value="1"></el-option>
            </el-select>
                    </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" >提交</el-button>
        </div>
    </el-dialog>
    </section>
</template>

<script>
import util from '../common/js/util.js'

export default {
  data () {
    return {
      filters: {
        name: ''
      },
      list: [],
      total: 0,
      pageSize: 10,
      page: 1,
      sels: [], // 列表选中列

      editFormVisible: false, // 编辑界面是否显示
      // 编辑界面数据
      editForm: {
      },

      addFormVisible: false, // 新增界面是否显示
      // 新增界面数据
      addForm: {
      },

      formRules: {
        password: [
          { required: true, message: '请输入password', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请输入username', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {

    getIndex: function (index) {
      return index + (this.page - 1) * this.pageSize
    },
    // 性别显示转换
    formatSex: function (row, column) {
      return row.sex === '0' ? '男' : row.sex === '1' ? '女' : '未知'
    },

    handleCurrentChange (val) {
      this.page = val
      this.getUsers()
    },
    // 获取用户列表
    getUsers () {
      let para = {
        page: this.page,
        pageSize: this.dd.pageSize,
        name: this.filters.name
      }

      this.api.getUserListPage(para).then((res) => {
        // debugger;
        this.total = res.data.data.totalSize
        this.pageSize = res.data.data.pageSize
        this.page = res.data.data.page
        this.list = res.data.data.list
      })
    },
    // 删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        let para = { id: row.id }
        this.api.removeUser(para).then((res) => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getUsers()
        })
      }).catch(() => {

      })
    },
    // 显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true
      this.editForm = Object.assign({}, row)
    },
    // 显示新增界面
    handleAdd: function () {
      this.addFormVisible = true
      this.addForm = {
        name: '',
        sex: 0,
        age: 0,
        birth: '',
        addr: ''
      }
    },

    // 编辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            let para = Object.assign({}, this.editForm)
            para.birth = (!para.birth || para.birth === '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd')
            this.api.editUser(para).then((res) => {
              this.$message({
                message: '提交成功',
                type: 'success'
              })
              this.$refs['editForm'].resetFields()
              this.editFormVisible = false
              this.getUsers()
            })
          })
        }
      })
    },
    // 新增
    addSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            let para = Object.assign({}, this.addForm)
            para.birth = (!para.birth || para.birth === '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd')
            this.api.addUser(para).then((res) => {
              this.$message({
                message: '提交成功',
                type: 'success'
              })
              this.$refs['addForm'].resetFields()
              this.addFormVisible = false
              this.getUsers()
            })
          })
        }
      })
    },
    selsChange: function (sels) {
      this.sels = sels
    },
    selsChange2: function (sels) {
      this.sels2 = sels
    },
    // 批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id)
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        let para = { ids: ids }
        this.api.batchRemoveUser(para).then((res) => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getUsers()
        })
      }).catch(() => {

      })
    }
  },
  mounted () {
    this.getUsers()
  }
}

</script>

<style scoped>
</style>
